package com.home766.controller.admin;

import com.home766.entity.Customer;
import com.home766.service.ICustomerService;
import com.home766.util.StringUtil;
import net.biz.Message;
import net.biz.controller.admin.BaseController;
import net.biz.entity.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/admin/customer")
public class CustomerController extends BaseController {
	@Autowired
	private ICustomerService customerService;
	
	/**
	 * 查询客服联系
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String searchAll(ModelMap model){
		List<Customer> list= customerService.findAll();
		boolean bool=true;//是否可以添加
		model.addAttribute("list", list);
		if(list!=null&&!list.isEmpty()){
			bool=false;
			model.addAttribute("total",list.size());
		}else{
			model.addAttribute("total", 0);
		}
		model.addAttribute("addCheck",bool);
		return "/admin/system/list";
	}
	/**
	 * 新增跳转
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String add(ModelMap model){
		setUtil(model);
		return "/admin/system/add"; 
	}
	/**
	 * 修改跳转
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/edit",method=RequestMethod.GET)
	public String edit(Long id, ModelMap model){
		String list="redirect:list.jhtml";
		if(null==id){
			return list;
		}
		Customer customer= customerService.find(id);
		model.addAttribute("customer", customer);
		setUtil(model);
		return "/admin/system/edit"; 
	}
	/**
	 * 查看
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/show",method=RequestMethod.GET)
	public String show(Long id, ModelMap model){
		String list="redirect:list.jhtml";
		if(null==id){
			return list;
		}
		Customer customer= customerService.find(id);
		model.addAttribute("customer", customer);
		return "/admin/system/show"; 
	}
	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Message delete(Long[] ids) {
		customerService.delete(ids);
		return SUCCESS_MESSAGE;
	}
	
	/**
	 * 新增
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public String save(Customer customer, ModelMap model, RedirectAttributes redirectAttributes){
		String add="/admin/system/add";
		model.addAttribute("customer", customer);
		if(customer==null){
			return add;
		}
		if(null==customer.getTencentId1()||"".equals(customer.getTencentId1())){
			return add;
		}
		if(null==customer.getTel()||"".equals(customer.getTel())){
			return add;
		}
		if(null==customer.getWorkTime()||"".equals(customer.getWorkTime())){
			return add;
		}
		setUtilEntity(customer);
		customerService.save(customer);
		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		return "redirect:list.jhtml"; 
	}
	
	/**
	 * 修改
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public String update(Customer customer, ModelMap model, RedirectAttributes redirectAttributes){
		String add="/admin/system/add";
		model.addAttribute("customer", customer);
		if(customer==null){
			return add;
		}
		if(null==customer.getTencentId1()||"".equals(customer.getTencentId1())){
			return add;
		}
		if(null==customer.getTel()||"".equals(customer.getTel())){
			return add;
		}
		if(null==customer.getWorkTime()||"".equals(customer.getWorkTime())){
			return add;
		}
		setUtilEntity(customer);
		customerService.update(customer);
		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		return "redirect:list.jhtml"; 
	}
	private void setUtil(ModelMap model){
		model.addAttribute("staffName", "");
		model.addAttribute("staffSn", "");
		Admin admin=getCurrent();
		if(null!=admin){
			if(!StringUtil.isNullOrSpace(admin.getName())){
				model.addAttribute("staffName", admin.getName());
			}
			if(null!=admin.getId()){
				model.addAttribute("staffSn", admin.getId());
			}
		}
	}
	private void setUtilEntity(Customer customer){
		Admin admin=getCurrent();
		if(null!=admin){
			if(!StringUtil.isNullOrSpace(admin.getName())){
				customer.setStaffName(admin.getName());
			}
			if(null!=admin.getId()){
				customer.setStaffSn(String.valueOf(admin.getId()));
			}
		}
	}
	
}
